import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 优选算法：day1
 * User: 姚东名
 * Date: 2024-11-04
 * Time: 21:46
 */
public class Test {
    //1. 移动零（easy）
    public void moveZeroes(int[] nums) {
        int cur = 0;
        int dest = -1;
        for (; cur < nums.length; cur++) {
            if (nums[cur] != 0) {
                Swap(nums, dest + 1, cur);
                dest++;
            }
        }
    }

    private void Swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    //2. 复写零
    public void duplicateZeros(int[] arr) {
        int cur = 0;
        int dest = -1;
        int len = arr.length;
        // 1. 找到最后一个需要“复写”的数
        while (cur <= len - 1) {
            if (arr[cur] == 0) {
                dest += 2;
            } else {
                dest += 1;
            }
            if (dest >= len - 1) {
                break;
            }
            cur++;
        }
        // 2. 处理dest == len的情况（要不是在len - 1 就是在len位置）
        if (dest == len) {
            arr[dest - 1] = 0;
            cur--;
            dest -= 2;
        }
        // 3. 从后往前遍历
        while (cur >= 0) {
            if (arr[cur] != 0) {
                arr[dest--] = arr[cur--];
            } else {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }

    //3. 快乐数
    //计算它每个位置上的数字的平方和
    public int bitSum(int n) {
        int sum = 0;
        while (n != 0) {
            int t = n % 10;
            sum += t * t;
            n = n / 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        int slow = n;
        int fast = bitSum(n);
        //求出快慢指针相遇的数字
        while (slow != fast) {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }

    // 4. 盛最多水的容器
    //1. 暴力解法：
    public int maxArea2(int[] height) {
        int n = height.length - 1;
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                // 计算容积，找出最⼤的那⼀个
                //ret = max(ret, min(height[i], height[j]) * (j - i));
                int V = Math.min(height[i], height[j]) * (j - i);
                ret = Math.max(ret, V);
            }
        }
        return ret;
    }

    //2. 利用单调性，双指针来解决问题
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int ret = 0;// 储存的最大水量
        while (left < right) {
            int V = Math.min(height[left], height[right]) * (right - left);
            ret = Math.max(ret, V);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return ret;
    }

    //5. 有效三角形的个数
    public int triangleNumber(int[] nums) {
        //1. 先给数组从小到大排序
        Arrays.sort(nums);
        //2. 利用双指针来解决问题
        int ret = 0;
        int n = nums.length - 1;
        for (int i = n; i >= 2; i--) {
            //先固定一个最大的数
            int left = 0, right = i - 1;
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    ret += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        return ret;
    }
}